<?php
namespace App\Http\Controllers;

use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\MatlabModule;
use App\Http\Controllers\Business\StatisticModule;
use App\Http\Controllers\Business\WaterModule;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;

class IndexController extends WebController
{
    //登录
    public function index() {
        $result = StatisticModule::index();
        $data = $result['data'];
        return view('index', $data);
    }
    public function firstClassNodeWaterMeterList() {
        $result = WaterModule::firstNodeLevelWaterMeterList();
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }

    public function makeMatlab( ) {
        return view('matlab');
    }
    public function processMatlab() {
        $Q = $this->request->get('Q');
        $D = $this->request->get('D');
        if(!$Q || !$D) {
            return $this->responseError('请输入数据');
        }
        if(!strpos($Q, ';')) {
            return $this->responseError('Q数据格式有问题');
        }
        if(!strpos($D, ',')) {
            return $this->responseError('D数据格式有问题');
        }
        $matlabModule = new MatlabModule();
        //$result = $matlabModule->suanFa($Q, $D);
        $data = $matlabModule->suanFaData();
        //if($result == false) {
        //    return false;
        //}
        //$F = $result['F'];
        //$D = $result['D'];
        //$data = $result['data'];

        $title = ["液位(mm)","容量(L)","液位(mm)","容量(L)","液位(mm)","容量(L)","液位(mm)","容量(L)","液位(mm)","容量(L)","液位(mm)","容量(L)","液位(mm)","容量(L)","液位(mm)","容量(L)","液位(mm)","容量(L)","液位(mm)","容量(L)"];
        $cellData = [];
        array_push($cellData, $title);
        foreach($data as $list) {
            array_push($cellData, $list);
        }
        $title = '流量数据';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('流量数据', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');

    }
}